package com.qf.lanzhou.blog.api.service.impl.login;

import com.qf.lanzhou.blog.api.dao.Impl.login.UserDaoImpl;
import com.qf.lanzhou.blog.api.dao.UserDao;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.LoginService;
import com.qf.lanzhou.blog.api.util.MD5Utils;

/**
 * @author LEON-HE
 */
public class LoginServiceImpl implements LoginService {
    private UserDao userDao = new UserDaoImpl();
    @Override
    public User checkLogin(User loginUser) {
        User dbUser = userDao.selectUserByUserName(loginUser.getUserName());
        String enPassword = MD5Utils.stringToMD5(loginUser.getPassword());
        if (dbUser==null || "".equals(dbUser)){
            throw new RuntimeException("用户不存在");
        }else if (!dbUser.getPassword().equals(enPassword)){
            throw new RuntimeException("密码不正确");
        }
        if (!userDao.updateLoginTime(dbUser.getUserName())){
            throw new RuntimeException("更新登录时间失败");
        }
        return dbUser;
    }
}
